package com.cloudservice.ccs.biz.impl;

import com.cloudservice.ccs.biz.ICustomerServiceBiz;
import com.cloudservice.ccs.entity.CustomerService;
import com.cloudservice.ccs.exception.CustomerServiceException;
import com.cloudservice.ccs.mapper.CustomerServiceMapper;
import com.cloudservice.ccs.model.HomeTable;
import jBCrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements ICustomerServiceBiz {

    @Autowired
    CustomerServiceMapper customerServiceMapper;

    @Override
    public void save(CustomerService customerService) {
        if (customerService.getPassword() != null) {
            hashPasswordOf(customerService);
        }
        int result = customerServiceMapper.save(customerService);
        if (result == 0) {
            throw new CustomerServiceException("创建客服失败");
        }
    }

    @Override
    public void update(Integer id, CustomerService customerService) {
        if (customerService.getPassword() != null) {
            CustomerService originalCustomerService = customerServiceMapper.getOne(id);
            if (originalCustomerService == null) {
                throw new CustomerServiceException("要更新的客服不存在");
            }
            String originalPassword = originalCustomerService.getPassword();
            String newPassword = customerService.getPassword();
            if (originalPassword != null && newPassword != null && !originalPassword.equals(newPassword)) {
                hashPasswordOf(customerService);
            }
        }
        int result = customerServiceMapper.update(id, customerService);
        if (result == 0) {
            throw new CustomerServiceException("更新客服信息失败");
        }
    }

    @Override
    public void partialUpdate(Integer id, Map<String, Object> fields) {
        if (fields.containsKey("password") && fields.get("password") != null) {
            fields.put("password", hashOf((String) fields.get("password")));
        }
        int result = customerServiceMapper.partialUpdate(id, fields);
        if (result == 0) {
            throw new CustomerServiceException("更新客服信息失败");
        }
    }

    @Override
    public void delete(Integer id) {
        int result = customerServiceMapper.delete(id);
        if (result == 0) {
            throw new CustomerServiceException("删除客服失败");
        }
    }

    @Override
    public CustomerService getOne(Integer id) {
        CustomerService customerService = customerServiceMapper.getOne(id);
        if (customerService == null) {
            throw new CustomerServiceException("要查询的客服不存在");
        }
        return customerService;
    }

    @Override
    public List<CustomerService> getAll() {
        return customerServiceMapper.getAll();
    }

    private void hashPasswordOf(CustomerService customerService) {
        customerService.setPassword(hashOf(customerService.getPassword()));
    }

    private String hashOf(String password) {
        String salt = BCrypt.gensalt();
        return BCrypt.hashpw(password, salt);
    }

    @Override
    public List<HomeTable> getHomeTable() {
        return customerServiceMapper.getHomeTable();
    }

}
